package y2015.date0930放假倒计时窗体;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DaojishiWin extends JFrame {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JLabel timeLabel, secondLabel;
    JLabel nowtimeLabel, daotimeLabel;
    Box baseBox;
    Font fontHei, fontSong;

    public DaojishiWin() {
        setTitle("放假了~");
        setSize(300, 300);
        timeLabel = new JLabel();
        secondLabel = new JLabel();
        nowtimeLabel = new JLabel("当前时间：");
        daotimeLabel = new JLabel("距离2016年10月1日倒计时：");
        fontHei = new Font("黑体", Font.PLAIN, 20);
        fontSong = new Font("宋体", Font.PLAIN, 16);
        nowtimeLabel.setFont(fontHei);
        timeLabel.setFont(fontSong);
        daotimeLabel.setFont(fontHei);
        secondLabel.setFont(fontSong);
        baseBox = Box.createVerticalBox();
        //		baseBox.setLayout(new GridLayout(2,1));
        baseBox.add(nowtimeLabel);
        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(timeLabel);

        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(daotimeLabel);

        baseBox.add(Box.createVerticalStrut(20));
        baseBox.add(secondLabel);
        add(baseBox);
        JOptionPane dialog = new JOptionPane(JOptionPane.INFORMATION_MESSAGE);
        init();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    private void init() {
        Thread tread = new Thread(new Runnable() {
            @Override
            public void run() {
                String text = null;
                int daotime;
                while (true) {
                    try {
                        Thread.sleep(1000);
                        text = nowTime();
                        daotime = (int) daoTime();
                        timeLabel.setText("   " + text);
                        secondLabel.setText("   " + daotime + " 秒");

                        switch (daotime) {
                            case 0:
                                nowtimeLabel.setText("");
                                daotimeLabel.setText("");
                                timeLabel.setText("");
                                Font font = new Font("黑体", Font.PLAIN, 30);
                                secondLabel.setFont(font);
                                secondLabel.setText("**国庆节到了**");
                                break;
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        tread.start();
    }

    private String nowTime() {
        Date time = new Date();
        SimpleDateFormat geshi = new SimpleDateFormat("MM月dd日 HH:mm:ss");
        String nowtime = geshi.format(time);
        return nowtime;
    }

    private long daoTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2016, 8, 30, 17, 18, 0);//设置时间为2016年10月1日00:00
        long systemSeconds = System.currentTimeMillis() / 1000;
        long setSeconds = calendar.getTimeInMillis() / 1000;
        long seconds = setSeconds - systemSeconds;

        return seconds;
    }
}

